package Java7_Concurrency_Cookbook.thread_synchronized_helper.count_down_latch;

import java.util.concurrent.CountDownLatch;

/**
 * 视频会议对象
 * @author lixiangke
 */
public class VideoConference implements Runnable {

    private final CountDownLatch controller;

    public VideoConference(int number) {
        this.controller = new CountDownLatch(number);
    }

    public void arrive(String name) {
        System.out.println(name + " has arrived");
        controller.countDown();
        System.out.println("VideoConference: Wait for " + controller.getCount() + " participants");
    }

    @Override
    public void run() {
        System.out.println("VideoConference: Initialization: " + controller.getCount() + " participants.");
        try {
            controller.await();
            System.out.println("VideoConference: All the participants have come");
            System.out.println("VideoConference: Let's start...");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
